<template>
  <MainCard :show-bg="false" :class="[appStore.isDeep ? '' : 'p-0']">
    <TestStep
      ref="$TestStep"
      v-model="step"
      v-model:show="show"
      :type="type"
      :is-evaluate="isEvaluate"
    >
      <ObjectSelect
        v-show="step === 1 || step === 2"
        :step="step"
        :import-object-name="importObjectName"
        @change="handleChange"
        @next="handleNext"
        @prev="handlePrev"
      />
      <SubjectForm
        v-if="step === 3 || step === 4"
        :step="step"
        :init-data="currentObject"
        :import-json-data="importJsonData"
        @create="handleCreate"
        @next="handleNext"
        @prev="handlePrev"
        @is-evaluate="handleIsEvaluate"
      />
      <StartTest v-if="step === 5" :id="testId" @next="handleNext" @prev="handlePrev" />
      <ExecuteTest v-if="step === 6" :id="testId" @next="handleNext" @prev="handlePrev" />
      <EvaluateTest
        v-if="step === 7"
        :id="testId"
        @next="handleNext"
        @prev="handlePrev"
        @report="handleReport"
      />
      <EvaluateReport v-if="step === 8" :id="executeId" @loading="handleLoading" />
    </TestStep>
  </MainCard>
</template>
<script setup lang="jsx">
import * as testRecordApi from '@/api/testRecord'
import TestStep from './modules/TestStep.vue'
import ObjectSelect from './modules/ObjectSelect.vue'
import SubjectForm from './modules/SubjectForm.vue'
import StartTest from './modules/StartTest.vue'
import ExecuteTest from './modules/ExecuteTest.vue'
import EvaluateTest from './modules/EvaluateTest.vue'
import EvaluateReport from './modules/EvaluateReport.vue'
import { useAppStore } from '@/store'
import { notEvaluateSimulatorValues } from '@/enum/simulator'
defineOptions({
  name: 'TestDetail',
})
const appStore = useAppStore()
const route = useRoute()
const step = ref(1)
const type = ref('static')
const $TestStep = ref(null)

function handlePrev() {
  step.value--
  nextTick(() => {
    $TestStep.value.scrollTo({ top: 0 })
  })
}
function handleNext() {
  step.value++
  nextTick(() => {
    $TestStep.value.scrollTo({ top: 0 })
  })
}

function handleLoading(val) {
  show.value = val
}
const testId = ref(null)
function handleCreate(id) {
  testId.value = id
}

const executeId = ref(null)
function handleReport(id) {
  executeId.value = id
}
const currentObject = ref(null)
function handleChange(current) {
  currentObject.value = current
  type.value = current.testModeId === '2' ? 'static' : 'dynamic'
}

const isEvaluate = ref(true)
function handleIsEvaluate(e) {
  isEvaluate.value = e
}

const show = ref(false)
async function getDetail(id) {
  testId.value = id
  show.value = true
  const {
    data: { testIndicatorCollection, evaluationIndicatorCollection, ...other },
  } = await testRecordApi.info(id)
  other['testIndicatorCollection'] = testIndicatorCollection?.length
    ? testIndicatorCollection.split(',')
    : []
  other['evaluationIndicatorCollection'] = evaluationIndicatorCollection?.length
    ? evaluationIndicatorCollection.split(',')
    : []
  if (other.startupState === '0') {
    step.value = Number(5)
  } else if (['1', '2', '4'].includes(other.startupState)) {
    step.value = Number(6)
  } else if (other.startupState === '3') {
    if (notEvaluateSimulatorValues.includes(other.simulator)) {
      step.value = Number(6)
    } else {
      step.value = Number(7)
    }
  } else {
    step.value = Number(1)
  }
  handleChange(other)
  show.value = false
}
const importObjectName = ref(null)
const importJsonData = ref(null)
function handleImport(data) {
  importObjectName.value = data['testObjectId'] ?? null
  importJsonData.value = data ?? null
}
onBeforeMount(() => {
  // 详情
  if (route.query.id) {
    step.value = Number(0)
    getDetail(route.query.id)
    return
  }
  // 导入
  if (history?.state?.importData) {
    handleImport(history.state.importData)
    return
  }
  // 评估报告
  if (route.query.executeId) {
    executeId.value = route.query.executeId
    step.value = Number(8)
    return
  }
})
</script>
<style lang="scss" scoped></style>
